<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">

<link rel="stylesheet" href="/css/main.css?v=7.1.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.2">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.2" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="人帅也要多读书，多学习，多运动">
<meta property="og:type" content="website">
<meta property="og:title" content="费曼的门徒">
<meta property="og:url" content="http://www.wuxiantao123.cn/index.html">
<meta property="og:site_name" content="费曼的门徒">
<meta property="og:description" content="人帅也要多读书，多学习，多运动">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="费曼的门徒">
<meta name="twitter:description" content="人帅也要多读书，多学习，多运动">





  
  
  <link rel="canonical" href="http://www.wuxiantao123.cn/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>费曼的门徒</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">费曼的门徒</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">Feynman follower</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home menu-item-active">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-备忘录">

    
    
    
      
    

    

    <a href="/books" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i> <br>备忘录</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-计划待办">

    
    
    
      
    

    

    <a href="/todolist" rel="section"><i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>计划待办</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    
  
  

  

  <a href="https://github.com/peach-boy" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewbox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a>



    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/08/05/Mysql学习笔记之查询优化篇/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/08/05/Mysql学习笔记之查询优化篇/" class="post-title-link" itemprop="url">Mysql学习笔记之查询优化篇</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-08-05 16:26:38" itemprop="dateCreated datePublished" datetime="2019-08-05T16:26:38+08:00">2019-08-05</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-08-22 22:41:28" itemprop="dateModified" datetime="2019-08-22T22:41:28+08:00">2019-08-22</time>
              
            
          </span>

          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/08/05/Mysql学习笔记之事务/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/08/05/Mysql学习笔记之事务/" class="post-title-link" itemprop="url">Mysql学习笔记之事务</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-08-05 16:25:58" itemprop="dateCreated datePublished" datetime="2019-08-05T16:25:58+08:00">2019-08-05</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-08-22 22:41:28" itemprop="dateModified" datetime="2019-08-22T22:41:28+08:00">2019-08-22</time>
              
            
          </span>

          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/08/05/Mysql学习笔记之锁/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/08/05/Mysql学习笔记之锁/" class="post-title-link" itemprop="url">Mysql学习笔记之锁</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-08-05 16:25:47" itemprop="dateCreated datePublished" datetime="2019-08-05T16:25:47+08:00">2019-08-05</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-08-22 22:41:28" itemprop="dateModified" datetime="2019-08-22T22:41:28+08:00">2019-08-22</time>
              
            
          </span>

          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/08/05/Mysql学习笔记之索引篇/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/08/05/Mysql学习笔记之索引篇/" class="post-title-link" itemprop="url">Mysql学习笔记之索引篇</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-08-05 16:25:34" itemprop="dateCreated datePublished" datetime="2019-08-05T16:25:34+08:00">2019-08-05</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-08-22 22:41:28" itemprop="dateModified" datetime="2019-08-22T22:41:28+08:00">2019-08-22</time>
              
            
          </span>

          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/07/09/Mysql学习笔记之explain用法详解/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/07/09/Mysql学习笔记之explain用法详解/" class="post-title-link" itemprop="url">Mysql学习笔记之explain用法详解</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-07-09 16:27:54" itemprop="dateCreated datePublished" datetime="2019-07-09T16:27:54+08:00">2019-07-09</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-07-17 11:07:34" itemprop="dateModified" datetime="2019-07-17T11:07:34+08:00">2019-07-17</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/mysql/" itemprop="url" rel="index"><span itemprop="name">mysql</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="explain的作用"><a href="#explain的作用" class="headerlink" title="explain的作用"></a>explain的作用</h3><p>用来获取查询执行计划的信息，当执行查询时，其返回关于查询计划的每一步的信息，而不是执行它。</p>
<h3 id="explain输出的列"><a href="#explain输出的列" class="headerlink" title="explain输出的列"></a>explain输出的列</h3><p>explain的输出总是相同的列，总共11列</p>
<h4 id="id"><a href="#id" class="headerlink" title="id"></a>id</h4><h4 id="select-type"><a href="#select-type" class="headerlink" title="select_type"></a>select_type</h4><p>显示对应行时简单查询还是复杂查询（如果是后者，那么是三种复杂类型中的哪一种）<br>取值：</p>
<ol>
<li>simple:简单查询，不包含子查询和union </li>
<li>primary:查询为复杂查询，最外层标记为PRIMARY</li>
<li>subquery:包含在select列表中的select子查询（不在from中的子句的子查询）标记为subquery</li>
<li>dependent subquery</li>
<li>derived:在from子句中的子查询</li>
<li>union:</li>
<li>union result:</li>
<li>dependent union</li>
<li></li>
</ol>
<h4 id="table"><a href="#table" class="headerlink" title="table"></a>table</h4><p>table表示对应行正在访问那个表，通常情况下，它就是表明或者别名</p>
<h4 id="type"><a href="#type" class="headerlink" title="type"></a>type</h4><p>访问类型，或者决定如何查找表中的行，依次从最差到最优。</p>
<p><code>all &lt; index &lt; range &lt; index_subquery &lt; unique_subquery &lt; index_merge &lt; ref_or_null &lt; ref &lt; eq_ref &lt; const&lt;system</code></p>
<ol>
<li>all:按行全表扫描，mysql必须扫描整张表，去找到需要的行（例外：limit会从开始扫描到下界加偏移量，如limit 100,10，会从开始扫描到110行;Extra列中有Using distinct/not exists时例外）</li>
<li>index:按索引次序扫描全表，需要遍历全表索引</li>
<li>range:范围扫描，只需遍历筛选后的区间的索引，一般带有between或者where的查询</li>
<li>range ： 这个是index了范围限制，例如 &gt;100、&lt;1000之类的查询条件，这样避免的index的全索引扫描，当然限制的范围越小 效率就越高。</li>
<li>index_subquery ： 在 某 些 IN 查 询 中 使 用 此 种 类 型 , 与 unique_subquery 类似,但是查询的是非唯一 性索引</li>
<li>unique_subquery ： 在某些 IN 查询中使用此种类型,而不是常规的 ref</li>
<li>index_merge ： 说明索引合并优化被使用了</li>
<li>ref_or_null ： 如同 ref, 但是 MySQL 必须在初次查找的结果 里找出 null 条目,然后进行二次查找。</li>
<li>ref ： 使用了非唯一性索引进行数据的查找，例如：我们对用户表的用户名这一列建立了非唯一索引，因为用户名可以重复，当我们查找用户的时候select * from user where username=“xxx”的时候就出现了ref，使用非唯一索引查找数据。</li>
<li>eq_ref ： 这个就很好理解了，使用的唯一性索引进行数据查找，例如主键索引之类的。</li>
<li>const ： 通常情况下，将一个主键放置到where后面作为条件查询，mysql优化器就能把这次查询优化转化为一个常量，如何转化以及何时转化，这个取决于优化器。这个比eq_ref效率高一点。</li>
<li>system ： 表只有一行。不过这种情况下就没意义了。</li>
<li>NULL ： MySQL不用访问表或者索引就直接能到结果。</li>
</ol>
<h4 id="possible-keys"><a href="#possible-keys" class="headerlink" title="possible_keys"></a>possible_keys</h4><h4 id="key"><a href="#key" class="headerlink" title="key"></a>key</h4><h4 id="key-len"><a href="#key-len" class="headerlink" title="key_len"></a>key_len</h4><h4 id="ref"><a href="#ref" class="headerlink" title="ref"></a>ref</h4><p>rows列显示MySQL认为它执行查询时必须检查的行数。注意这是一个预估值</p>
<h4 id="rows"><a href="#rows" class="headerlink" title="rows"></a>rows</h4><h4 id="extra"><a href="#extra" class="headerlink" title="extra"></a>extra</h4>
          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/07/09/JSON使用经验之java对象与json字符串的互转问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/07/09/JSON使用经验之java对象与json字符串的互转问题/" class="post-title-link" itemprop="url">JSON使用经验之java对象与json字符串的互转问题</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-07-09 16:26:49" itemprop="dateCreated datePublished" datetime="2019-07-09T16:26:49+08:00">2019-07-09</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-07-15 21:44:40" itemprop="dateModified" datetime="2019-07-15T21:44:40+08:00">2019-07-15</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/json/" itemprop="url" rel="index"><span itemprop="name">json</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="java对象转json字符串"><a href="#java对象转json字符串" class="headerlink" title="java对象转json字符串"></a>java对象转json字符串</h2><h3 id="fastJson实现方法"><a href="#fastJson实现方法" class="headerlink" title="fastJson实现方法"></a>fastJson实现方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">SerializeConfig config = new SerializeConfig();</span><br><span class="line">        config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;</span><br><span class="line">        String json = JSON.toJSONString(app, config);</span><br><span class="line">        System.out.println(&quot;fastJson---&quot; + json);</span><br><span class="line"></span><br><span class="line">console result:</span><br><span class="line">        fastJson---&#123;&quot;app_id&quot;:431431,&quot;image_url&quot;:&quot;http://www.image.com/xxx.png&quot;,&quot;link_url&quot;:&quot;http://www.baidu.com&quot;,&quot;name&quot;:&quot;美团外卖&quot;&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Jackson实现方法"><a href="#Jackson实现方法" class="headerlink" title="Jackson实现方法"></a>Jackson实现方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">ObjectMapper mapper = new ObjectMapper();</span><br><span class="line">        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);</span><br><span class="line">        String json = mapper.writeValueAsString(app);</span><br><span class="line">        System.out.println(&quot;jackson--&quot; + json);</span><br><span class="line"></span><br><span class="line">console result:</span><br><span class="line">      jackson--&#123;&quot;app_id&quot;:431431,&quot;name&quot;:&quot;美团外卖&quot;,&quot;image_url&quot;:&quot;http://www.image.com/xxx.png&quot;,&quot;link_url&quot;:&quot;http://www.baidu.com&quot;&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Gson实现方法"><a href="#Gson实现方法" class="headerlink" title="Gson实现方法"></a>Gson实现方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"> GsonBuilder gsonBuilder = new GsonBuilder();</span><br><span class="line">        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);</span><br><span class="line">        Gson gson = gsonBuilder.create();</span><br><span class="line"></span><br><span class="line">        String json = gson.toJson(app);</span><br><span class="line">        System.out.println(&quot;Gson--&quot; + json);</span><br><span class="line"></span><br><span class="line">console result:</span><br><span class="line">      Gson--&#123;&quot;app_id&quot;:431431,&quot;name&quot;:&quot;美团外卖&quot;,&quot;image_url&quot;:&quot;http://www.image.com/xxx.png&quot;,&quot;link_url&quot;:&quot;http://www.baidu.com&quot;&#125;</span><br></pre></td></tr></table></figure>

<h2 id="json字符串转java对象"><a href="#json字符串转java对象" class="headerlink" title="json字符串转java对象"></a>json字符串转java对象</h2><h3 id="fastJson实现方法-1"><a href="#fastJson实现方法-1" class="headerlink" title="fastJson实现方法"></a>fastJson实现方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">App app1 = JSON.parseObject(jsonStr, App.class);</span><br></pre></td></tr></table></figure>

<h3 id="Jackson实现方法-1"><a href="#Jackson实现方法-1" class="headerlink" title="Jackson实现方法"></a>Jackson实现方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ObjectMapper mapper = new ObjectMapper();</span><br><span class="line">       mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);</span><br><span class="line">       App app = mapper.readValue(jsonStr, App.class);</span><br></pre></td></tr></table></figure>

<h3 id="Gson实现方法-1"><a href="#Gson实现方法-1" class="headerlink" title="Gson实现方法"></a>Gson实现方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Gson gson = new Gson();</span><br><span class="line">Person person = gson.fromJson(json, Person.class)</span><br></pre></td></tr></table></figure>


          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/07/01/java多线程之FutureTask-Future和Callable/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/07/01/java多线程之FutureTask-Future和Callable/" class="post-title-link" itemprop="url">java多线程之FutureTask</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-07-01 21:14:54 / 修改时间：22:51:51" itemprop="dateCreated datePublished" datetime="2019-07-01T21:14:54+08:00">2019-07-01</time>
            

            
              

              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/java多线程/" itemprop="url" rel="index"><span itemprop="name">java多线程</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="FutureTask和Future"><a href="#FutureTask和Future" class="headerlink" title="FutureTask和Future"></a>FutureTask和Future</h2><h3 id="Future"><a href="#Future" class="headerlink" title="Future"></a>Future</h3><h4 id="Future接口主要方法："><a href="#Future接口主要方法：" class="headerlink" title="Future接口主要方法："></a>Future接口主要方法：</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">boolean cancel(boolean mayInterruptIfRunning);</span><br><span class="line"></span><br><span class="line">boolean isCancelled();</span><br><span class="line"></span><br><span class="line">boolean isDone();</span><br><span class="line"></span><br><span class="line">V get() throws InterruptedException, ExecutionException;</span><br><span class="line"></span><br><span class="line">V get(long timeout, TimeUnit unit)</span><br><span class="line">    throws InterruptedException, ExecutionException, TimeoutException;</span><br></pre></td></tr></table></figure>

<h3 id="FutureTask"><a href="#FutureTask" class="headerlink" title="FutureTask"></a>FutureTask</h3><p>FutureTask是Future的实现类，它提供了对Future的基本实现。可使用FutureTask包装Callable或Runnable对象，因为FutureTask实现了Runnable，所以也可以将FutureTask提交给Executor</p>
<h4 id="FutureTask的几种状态"><a href="#FutureTask的几种状态" class="headerlink" title="FutureTask的几种状态"></a>FutureTask的几种状态</h4><p>FutureTask实现了Runnable和Future接口。因此，FutureTask可以交给<br>Executor执行，也可以由调用线程直接执行（FutureTask.run()）。根据FutureTask.run()方法被执行<br>的时机，FutureTask可以处于下面3种状态。</p>
<ol>
<li>未启动:FutureTask.run()方法还没有被执行之前，FutureTask处于未启动状态。当创建一<br>个FutureTask，且没有执行FutureTask.run()方法之前，这个FutureTask处于未启动状态。</li>
<li>已启动:FutureTask.run()方法被执行的过程中，FutureTask处于已启动状态。</li>
<li>已完成:此状态有三种情况<ul>
<li>正常结束：FutureTask.run()方法执行完后。</li>
<li>被取消：FutureTask.cancel（…）</li>
<li>异常结束： 执行FutureTask.run()方法时抛出异常而异常结束，FutureTask处于已完成状态</li>
</ul>
</li>
</ol>
<h4 id="FutureTask中get和cancel"><a href="#FutureTask中get和cancel" class="headerlink" title="FutureTask中get和cancel"></a>FutureTask中get和cancel</h4><ol>
<li>get方法：<code>V get() throws InterruptedException, ExecutionException;</code><ul>
<li>当FutureTask处于未启动或已启动状态时，执行FutureTask.get()方法将导致调用线程阻塞；</li>
<li>当FutureTask处于已完成状态，调用FutureTask.get()方法将导致调用线程立即返回结果或者抛出异常。<ol start="2">
<li>cancel方法: <code>boolean cancel(boolean mayInterruptIfRunning);</code></li>
</ol>
</li>
<li>当FutureTask处于未启动状态时，执行FutureTask.cancel()方法将导致此任务永远不会被执行；</li>
<li>当FutureTask处于已启动状态时，执行FutureTask.cancel（true）方法将以中断执行此任务线程的方式来试图停止任务；</li>
<li>当FutureTask处于已启动状态时，执行FutureTask.cancel（false）方法将不会对正在执行此任务的线程产生影响（让正在执行的任务运行完成）；</li>
<li>当FutureTask处于已完成状态时，执行FutureTask.cancel（…）方法将返回false。</li>
</ul>
</li>
<li>get和cancel执行示例图<br><img src="/source/images/jus/FutureTask1.png" alt="FutureTask的get和cancel的执行示意图.jpg"></li>
</ol>
<h4 id="FutureTask和Future的使用"><a href="#FutureTask和Future的使用" class="headerlink" title="FutureTask和Future的使用"></a>FutureTask和Future的使用</h4><p>Callable、Future、FutureTask一般都是和线程池配合使用的,以下是常用方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;T&gt; Future&lt;T&gt; submit(Callable&lt;T&gt; task);</span><br><span class="line">&lt;T&gt; Future&lt;T&gt; submit(Runnable task, T result);</span><br><span class="line">&lt;T&gt; List&lt;Future&lt;T&gt;&gt; invokeAll(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks)throws InterruptedException;</span><br><span class="line">&lt;T&gt; List&lt;Future&lt;T&gt;&gt; invokeAll(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks,long timeout, TimeUnit unit)throws  InterruptedException;</span><br></pre></td></tr></table></figure>

<ol>
<li><p>Callable+Future使用示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"> public class FutureTaskTest &#123;</span><br><span class="line">    static class CallableTask implements Callable&lt;String&gt; &#123;</span><br><span class="line">        private Integer sleepTime;</span><br><span class="line"></span><br><span class="line">        public CallableTask(Integer sleepTime) &#123;</span><br><span class="line">            this.sleepTime = sleepTime;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        @Override</span><br><span class="line">        public String call() throws Exception &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+&quot;--begin--&quot;+System.currentTimeMillis());</span><br><span class="line">            Thread.sleep(sleepTime);</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+&quot;-------end--&quot;+System.currentTimeMillis());</span><br><span class="line">            return Thread.currentThread().getName();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line"></span><br><span class="line">        ExecutorService executorService = Executors.newFixedThreadPool(3);</span><br><span class="line"></span><br><span class="line">        List&lt;Callable&lt;String&gt;&gt; callableList = new ArrayList&lt;&gt;();</span><br><span class="line">        callableList.add(new CallableTask(2000));</span><br><span class="line">        callableList.add(new CallableTask(5000));</span><br><span class="line">        callableList.add(new CallableTask(2000));</span><br><span class="line"></span><br><span class="line">        try &#123;</span><br><span class="line">            List&lt;Future&lt;String&gt;&gt; futures = executorService.invokeAll(callableList);</span><br><span class="line">            System.out.println(&quot;return future--&quot;+System.currentTimeMillis());</span><br><span class="line">            for (Future future : futures) &#123;</span><br><span class="line">                System.out.println(future.get() + &quot;--get---&quot;+System.currentTimeMillis());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(&quot;main thread compelete&quot;);</span><br><span class="line">        executorService.shutdown();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">console result:</span><br><span class="line">pool-1-thread-1--begin--1561992318002</span><br><span class="line">pool-1-thread-3--begin--1561992318002</span><br><span class="line">pool-1-thread-2--begin--1561992318002</span><br><span class="line">pool-1-thread-3-------end--1561992320003</span><br><span class="line">pool-1-thread-1-------end--1561992320003</span><br><span class="line">pool-1-thread-2-------end--1561992323003</span><br><span class="line">return future--1561992323003</span><br><span class="line">pool-1-thread-1--get---1561992323003</span><br><span class="line">pool-1-thread-2--get---1561992323004</span><br><span class="line">pool-1-thread-3--get---1561992323004</span><br></pre></td></tr></table></figure>
</li>
<li><p>Callable+FutureTask使用示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">public static class CallableThread implements Callable&lt;String&gt;</span><br><span class="line">&#123;</span><br><span class="line">    public String call() throws Exception</span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(&quot;进入CallableThread的call()方法, 开始睡觉, 睡觉时间为&quot; + System.currentTimeMillis());</span><br><span class="line">        Thread.sleep(10000);</span><br><span class="line">        return &quot;123&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">    </span><br><span class="line">public static void main(String[] args) throws Exception</span><br><span class="line">&#123;</span><br><span class="line">    ExecutorService es = Executors.newCachedThreadPool();</span><br><span class="line">    CallableThread ct = new CallableThread();</span><br><span class="line">    FutureTask&lt;String&gt; f = new FutureTask&lt;String&gt;(ct);</span><br><span class="line">    es.submit(f);</span><br><span class="line">    es.shutdown();</span><br><span class="line">        </span><br><span class="line">    Thread.sleep(5000);</span><br><span class="line">    System.out.println(&quot;主线程等待5秒, 当前时间为&quot; + System.currentTimeMillis());</span><br><span class="line">        </span><br><span class="line">    String str = f.get();</span><br><span class="line">    System.out.println(&quot;Future已拿到数据, str = &quot; + str + &quot;, 当前时间为&quot; + System.currentTimeMillis());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Callable+Future的方式，es.submit(ct)方法返回的Future，底层实现new出来的是一个FutureTask</p>
</li>
</ol>

          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/06/26/java多线程之synchronized-volatle和final关键字/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/06/26/java多线程之synchronized-volatle和final关键字/" class="post-title-link" itemprop="url">java多线程之synchronized,volatle和final关键字</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-06-26 22:53:03" itemprop="dateCreated datePublished" datetime="2019-06-26T22:53:03+08:00">2019-06-26</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-06-27 23:10:25" itemprop="dateModified" datetime="2019-06-27T23:10:25+08:00">2019-06-27</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/java多线程/" itemprop="url" rel="index"><span itemprop="name">java多线程</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="synchronized简介"><a href="#synchronized简介" class="headerlink" title="synchronized简介"></a>synchronized简介</h2><p>一个共享变量同时被多个线程读写时，会出现不一致问题。我们来看下面的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @Description: 同时1000个线程给count加一</span><br><span class="line"> * @Auther: ThomasWu</span><br><span class="line"> * @Date: 2019/6/27 21:43</span><br><span class="line"> * @Email:1414924381@qq.com</span><br><span class="line"> */</span><br><span class="line">public class SynchronizedTest &#123;</span><br><span class="line">    private static Integer count = 0;</span><br><span class="line"></span><br><span class="line">    public static class MyThread extends Thread &#123;</span><br><span class="line">        @Override</span><br><span class="line">        public void run() &#123;</span><br><span class="line">            addNum();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void addNum() &#123;</span><br><span class="line">        count++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        for (int i = 0; i &lt; 1000; i++) &#123;</span><br><span class="line">            MyThread thread = new MyThread();</span><br><span class="line">            thread.start();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        try &#123;</span><br><span class="line">            Thread.sleep(1000);</span><br><span class="line">        &#125; catch (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(&quot;result:&quot; + count);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">第一次运行：result:660</span><br><span class="line">第二次运行：result:660</span><br><span class="line">第三次运行：result:660</span><br><span class="line">每一次运行结果都不等于1000</span><br></pre></td></tr></table></figure>

<h2 id="synchronized使用的各种场景"><a href="#synchronized使用的各种场景" class="headerlink" title="synchronized使用的各种场景"></a>synchronized使用的各种场景</h2><table>
<thead>
<tr>
<th>位置</th>
<th>被锁住的对象</th>
<th>实例</th>
</tr>
</thead>
<tbody><tr>
<td>实例方法</td>
<td>类的实例对象</td>
<td><code>public synchronized void method{}</code></td>
</tr>
<tr>
<td>静态方法</td>
<td>类对象</td>
<td><code>public static synchronized void method{}</code></td>
</tr>
<tr>
<td>实例对象</td>
<td>类的实例对象</td>
<td><code>synchronized (this){}</code></td>
</tr>
<tr>
<td>class对象</td>
<td>类对象</td>
<td><code>synchronized (Demo.class){}</code></td>
</tr>
<tr>
<td>任意实例对象</td>
<td>实例对象</td>
<td><code>String loclk=&quot;&quot;;   synchronized (lock){}</code></td>
</tr>
</tbody></table>
<h3 id="synchronized用于实例方法上"><a href="#synchronized用于实例方法上" class="headerlink" title="synchronized用于实例方法上"></a>synchronized用于实例方法上</h3><p>示例代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line"> package syn;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @Description: TODO</span><br><span class="line"> * @Auther: ThomasWu</span><br><span class="line"> * @Date: 2019/6/27 22:25</span><br><span class="line"> * @Email:1414924381@qq.com</span><br><span class="line"> */</span><br><span class="line">public class LockObject &#123;</span><br><span class="line"></span><br><span class="line">    static class ThreadB extends Thread &#123;</span><br><span class="line">        private LockObject lockObject;</span><br><span class="line"></span><br><span class="line">        public ThreadB(LockObject lockObject) &#123;</span><br><span class="line">            super();</span><br><span class="line">            this.lockObject = lockObject;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        @Override</span><br><span class="line">        public void run() &#123;</span><br><span class="line">            super.run();</span><br><span class="line">            lockObject.methodB();</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    static class ThreadA extends Thread &#123;</span><br><span class="line">        private LockObject lockObject;</span><br><span class="line"></span><br><span class="line">        public ThreadA(LockObject lockObject) &#123;</span><br><span class="line">            super();</span><br><span class="line">            this.lockObject = lockObject;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        @Override</span><br><span class="line">        public void run() &#123;</span><br><span class="line">            super.run();</span><br><span class="line">            lockObject.methodA();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public synchronized void methodA() &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + &quot;--begin--&quot; + System.currentTimeMillis());</span><br><span class="line">            Thread.sleep(1000);</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + &quot;--end----&quot; + System.currentTimeMillis());</span><br><span class="line">        &#125; catch (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public synchronized void methodB() &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + &quot;--begin--&quot; + System.currentTimeMillis());</span><br><span class="line">            Thread.sleep(1000);</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + &quot;--end----&quot; + System.currentTimeMillis());</span><br><span class="line">        &#125; catch (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        LockObject lockObject = new LockObject();</span><br><span class="line"></span><br><span class="line">        ThreadA threadA = new ThreadA(lockObject);</span><br><span class="line">        threadA.setName(&quot;A&quot;);</span><br><span class="line"></span><br><span class="line">        ThreadB threadB = new ThreadB(lockObject);</span><br><span class="line">        threadB.setName(&quot;A&quot;);</span><br><span class="line"></span><br><span class="line">        threadA.start();</span><br><span class="line">        threadB.start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">result1 :methodA为同步方法，methodB为同步方法执行结果</span><br><span class="line">A--begin--1561647391094</span><br><span class="line">A--begin--1561647391094</span><br><span class="line">A--end----1561647392094</span><br><span class="line">A--end----1561647392094</span><br><span class="line"></span><br><span class="line">result2:methodB,methodA都为同步方法执行结果</span><br><span class="line">A--begin--1561647419478</span><br><span class="line">A--end----1561647420480</span><br><span class="line">A--begin--1561647420480</span><br><span class="line">A--end----1561647421480</span><br></pre></td></tr></table></figure>

<p>由此上面的实验可以得出：</p>
<ul>
<li>1、A线程持有Object对象的Lock锁，B线程可以以异步方式调用Object对象中的非synchronized类型的方法</li>
<li>2、A线程持有Object对象的Lock锁，B线程如果在这时调用Object对象中的synchronized类型的方法则需要等待，也就是同步</li>
</ul>

          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/06/26/java多线程之内存模型/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/06/26/java多线程之内存模型/" class="post-title-link" itemprop="url">java多线程之内存模型</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-06-26 21:54:31 / 修改时间：22:45:41" itemprop="dateCreated datePublished" datetime="2019-06-26T21:54:31+08:00">2019-06-26</time>
            

            
              

              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/java多线程/" itemprop="url" rel="index"><span itemprop="name">java多线程</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><h3 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h3><p>现代计算机存储设备与处理器的计算速度存在着几个数量级的差距，大量的时间花费在磁盘I/O或数据库访问上，所以现代计算机系统都不得不加入一层读写速度尽可能接近处理器速度的高速缓存来作为内存与处理器之间的缓中；将计算时需要使用的数据复制到缓存中，让计算速度快速进行，当计算结束后再从缓存同步到内存之中，这样处理器就无需等待缓慢的内存读写了。</p>
<h3 id="新的问题"><a href="#新的问题" class="headerlink" title="新的问题"></a>新的问题</h3><p>高速缓存解决了处理器与内存的速度矛盾，但却引入了一个新的问题-缓存不一致问题；多处理器系统中，每个处理器都有自己的高速缓存，而他们都共享同一个主内存，各自处理器计算完成后，需要将各自缓存的数据回写到主内存时，处理器之间缓存的数据不一致时就会导致不一致问题，java内存模型的出现即为解决这一问题</p>
<h3 id="什么是java内存模型"><a href="#什么是java内存模型" class="headerlink" title="什么是java内存模型"></a>什么是java内存模型</h3><p>不同的硬件生产商和不同的操作系统下，内存的访问逻辑有一定的差异，结果就是当你的代码在某个系统环境下运行良好，并且线程安全，但是换了个系统就出现各种问题。Java内存模型，就是为了屏蔽系统和硬件的差异，让一套代码在不同平台下能到达相同的访问结果。JMM从java 5开始的JSR-133发布后，已经成熟和完善起来。</p>
<h2 id="工作原理"><a href="#工作原理" class="headerlink" title="工作原理"></a>工作原理</h2><h3 id="内存划分"><a href="#内存划分" class="headerlink" title="内存划分"></a>内存划分</h3><p>JVM在设计时候考虑到，如果JAVA线程每次读取和写入变量都直接操作主内存，对性能影响比较大，所以每条线程拥有各自的工作内存，工作内存中的变量是主内存中的一份拷贝，线程对变量的读取和写入，直接在工作内存中操作，而不能直接去操作主内存中的变量。但是这样就会出现一个问题，当一个线程修改了自己工作内存中变量，对其他线程是不可见的，会导致线程不安全的问题。因为JMM制定了一套标准来保证开发者在编写多线程程序的时候，能够控制什么时候内存会被同步给其他线程。<br><img src="/source/images/jus/JMM1.png" alt="alt"></p>
<h3 id="内存之间的交互操作"><a href="#内存之间的交互操作" class="headerlink" title="内存之间的交互操作"></a>内存之间的交互操作</h3><p>内存交互操作有8种，虚拟机实现必须保证每一个操作都是原子的，不可在分的（对于double和long类型的变量来说，load、store、read和write操作在某些平台上允许例外）</p>
<ul>
<li>lock     （锁定）：作用于主内存的变量，把一个变量标识为线程独占状态</li>
<li>unlock （解锁）：作用于主内存的变量，它把一个处于锁定状态的变量释放出来，释放后的变量才可以被其他线程锁定</li>
<li>read    （读取）：作用于主内存变量，它把一个变量的值从主内存传输到线程的工作内存中，以便随后的load动作使用</li>
<li>load     （载入）：作用于工作内存的变量，它把read操作从主存中变量放入工作内存中</li>
<li>use      （使用）：作用于工作内存中的变量，它把工作内存中的变量传输给执行引擎，每当虚拟机遇到一个需要使用到变量的值，就会使用到这个指令</li>
<li>assign  （赋值）：作用于工作内存中的变量，它把一个从执行引擎中接受到的值放入工作内存的变量副本中</li>
<li>store    （存储）：作用于主内存中的变量，它把一个从工作内存中一个变量的值传送到主内存中，以便后续的write使用</li>
<li>write 　（写入）：作用于主内存中的变量，它把store操作从工作内存中得到的变量的值放入主内存的变量中</li>
</ul>
<p>　　JMM对这八种指令的使用，制定了如下规则：</p>
<ul>
<li>不允许read和load、store和write操作之一单独出现。即使用了read必须load，使用了store必须write</li>
<li>不允许线程丢弃他最近的assign操作，即工作变量的数据改变了之后，必须告知主存</li>
<li>不允许一个线程将没有assign的数据从工作内存同步回主内存</li>
<li>一个新的变量必须在主内存中诞生，不允许工作内存直接使用一个未被初始化的变量。就是怼变量实施use、store操作之前，必须经过assign和load操作</li>
<li>一个变量同一时间只有一个线程能对其进行lock。多次lock后，必须执行相同次数的unlock才能解锁</li>
<li>如果对一个变量进行lock操作，会清空所有工作内存中此变量的值，在执行引擎使用这个变量前，必须重新load或assign操作初始化变量的值</li>
<li>如果一个变量没有被lock，就不能对其进行unlock操作。也不能unlock一个被其他线程锁住的变量</li>
<li>对一个变量进行unlock操作之前，必须把此变量同步回主内存</li>
</ul>
<p>JMM对这八种操作规则和对volatile的一些特殊规则就能确定哪里操作是线程安全，哪些操作是线程不安全的了。但是这些规则实在复杂，很难在实践中直接分析。所以一般我们也不会通过上述规则进行分析。更多的时候，使用java的happen-before规则来进行分析。</p>
<h2 id="Happen-Before（先行发生规则）"><a href="#Happen-Before（先行发生规则）" class="headerlink" title="Happen-Before（先行发生规则）"></a>Happen-Before（先行发生规则）</h2><p>　　在常规的开发中，如果我们通过上述规则来分析一个并发程序是否安全，估计脑壳会很疼。因为更多时候，我们是分析一个并发程序是否安全，其实都依赖Happen-Before原则进行分析。Happen-Before被翻译成先行发生原则，意思就是当A操作先行发生于B操作，则在发生B操作的时候，操作A产生的影响能被B观察到，“影响”包括修改了内存中的共享变量的值、发送了消息、调用了方法等。</p>
<p>　　Happen-Before的规则有以下几条</p>
<ul>
<li>程序次序规则（Program Order Rule）：在一个线程内，程序的执行规则跟程序的书写规则是一致的，从上往下执行。</li>
<li>管程锁定规则（Monitor Lock Rule）：一个Unlock的操作肯定先于下一次Lock的操作。这里必须是同一个锁。同理我们可以认为在synchronized同步同一个锁的时候，锁内先行执行的代码，对后续同步该锁的线程来说是完全可见的。</li>
<li>volatile变量规则（volatile Variable Rule）：对同一个volatile的变量，先行发生的写操作，肯定早于后续发生的读操作</li>
<li>线程启动规则（Thread Start Rule）：Thread对象的start()方法先行发生于此线程的没一个动作</li>
<li>线程中止规则（Thread Termination Rule）：Thread对象的中止检测（如：Thread.join()，Thread.isAlive()等）操作，必行晚于线程中所有操作</li>
<li>线程中断规则（Thread Interruption Rule）：对线程的interruption（）调用，先于被调用的线程检测中断事件(Thread.interrupted())的发生</li>
<li>对象中止规则（Finalizer Rule）：一个对象的初始化方法先于一个方法执行Finalizer()方法</li>
<li>传递性（Transitivity）：如果操作A先于操作B、操作B先于操作C,则操作A先于操作C</li>
</ul>
<p>　　以上就是Happen-Before中的规则。通过这些条件的判定，仍然很难判断一个线程是否能安全执行，毕竟在我们的时候线程安全多数依赖于工具类的安全性来保证。想提高自己对线程是否安全的判断能力，必然需要理解所使用的框架或者工具的实现，并积累线程安全的经验。</p>
<p>参考：</p>
<ul>
<li><a href="http://tutorials.jenkov.com/java-concurrency/java-memory-model.html" target="_blank" rel="noopener">http://tutorials.jenkov.com/java-concurrency/java-memory-model.html</a></li>
<li>深入理解java虚拟第12张</li>
</ul>

          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/06/22/java多线程之并发工具类/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                
                <a href="/2019/06/22/java多线程之并发工具类/" class="post-title-link" itemprop="url">java多线程之并发工具类</a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-06-22 20:41:00" itemprop="dateCreated datePublished" datetime="2019-06-22T20:41:00+08:00">2019-06-22</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-06-23 16:06:01" itemprop="dateModified" datetime="2019-06-23T16:06:01+08:00">2019-06-23</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/java多线程/" itemprop="url" rel="index"><span itemprop="name">java多线程</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="CountDownLatch"><a href="#CountDownLatch" class="headerlink" title="CountDownLatch"></a>CountDownLatch</h2><ul>
<li>CountDownLatch的作用：允许一个或多个线程，等待其他线程达到触发条件时（触发条件即CountDownLatch对象计数器减为0时）才开始执行</li>
<li>主要方法<ol>
<li>CountDownLatch构造函数：CountDownLatch的构造函数接收一个int类型的参数作为计数器，如果你想等待N个点完<br>成，这里就传入N</li>
<li>countDown：当我们调用CountDownLatch的countDown方法时，N就会减1</li>
<li>await:CountDownLatch的await方法<br>会阻塞当前线程，直到N变成零,当前线程才会执行；await（long time，TimeUnit unit）这个方法等待特定时<br>间后，就会不再阻塞当前线程</li>
</ol>
</li>
<li>示例代码：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">**</span><br><span class="line"> * @Description: TODO</span><br><span class="line"> * @Auther: ThomasWu</span><br><span class="line"> * @Date: 2019/6/22 20:53</span><br><span class="line"> * @Email:1414924381@qq.com</span><br><span class="line"> */</span><br><span class="line">public class CountDownLatchTest &#123;</span><br><span class="line"></span><br><span class="line">    private static class WorkTask extends Thread &#123;</span><br><span class="line">        private CountDownLatch countDownLatch;</span><br><span class="line"></span><br><span class="line">        public WorkTask(String name, CountDownLatch countDownLatch) &#123;</span><br><span class="line">            super(name);</span><br><span class="line">            this.countDownLatch = countDownLatch;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        @Override</span><br><span class="line">        public void run() &#123;</span><br><span class="line">            try &#123;</span><br><span class="line">                System.out.println(this.getName() + &quot;启动了&quot;+System.currentTimeMillis());</span><br><span class="line">                Thread.sleep(1000);</span><br><span class="line">            &#125; catch (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125; finally &#123;</span><br><span class="line">                System.out.println(this.getName() + &quot;执行完了&quot;+System.currentTimeMillis());</span><br><span class="line">                countDownLatch.countDown();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private static class DoneTask extends Thread &#123;</span><br><span class="line">        private CountDownLatch countDownLatch;</span><br><span class="line"></span><br><span class="line">        public DoneTask(String name, CountDownLatch countDownLatch) &#123;</span><br><span class="line">            super(name);</span><br><span class="line">            this.countDownLatch = countDownLatch;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        @Override</span><br><span class="line">        public void run() &#123;</span><br><span class="line">            try &#123;</span><br><span class="line">                System.out.println(this.getName() + &quot;开始等待&quot;+System.currentTimeMillis());</span><br><span class="line">                countDownLatch.await();</span><br><span class="line">                System.out.println(this.getName() + &quot;执行完了&quot;+System.currentTimeMillis());</span><br><span class="line">            &#125; catch (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        CountDownLatch countDownLatch=new CountDownLatch(3);</span><br><span class="line">        ExecutorService service=Executors.newFixedThreadPool(5);</span><br><span class="line">        service.submit(new DoneTask(&quot;DonwTask1&quot;,countDownLatch));</span><br><span class="line">        service.submit(new DoneTask(&quot;DoneTask2&quot;,countDownLatch));</span><br><span class="line">        service.submit(new WorkTask(&quot;workTask1&quot;,countDownLatch));</span><br><span class="line">        service.submit(new WorkTask(&quot;workTask2&quot;,countDownLatch));</span><br><span class="line">        service.submit(new WorkTask(&quot;workTask3&quot;,countDownLatch));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">console result</span><br><span class="line">DonwTask1开始等待1561210606078</span><br><span class="line">DoneTask2开始等待1561210606078</span><br><span class="line">workTask2启动了1561210606079</span><br><span class="line">workTask1启动了1561210606079</span><br><span class="line">workTask3启动了1561210606079</span><br><span class="line">workTask3执行完了1561210607079</span><br><span class="line">workTask2执行完了1561210607079</span><br><span class="line">workTask1执行完了1561210607079</span><br><span class="line">DonwTask1执行完了1561210607079</span><br><span class="line">DoneTask2执行完了1561210607079</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="CyclicBarrier-同步屏障或栅栏"><a href="#CyclicBarrier-同步屏障或栅栏" class="headerlink" title="CyclicBarrier(同步屏障或栅栏)"></a>CyclicBarrier(同步屏障或栅栏)</h2><ul>
<li><p>CyclicBarrier作用:让一<br>组线程到达一个屏障（也可以叫同步点）时被阻塞，直到最后一个线程到达屏障时，屏障才会<br>开门，所有被屏障拦截的线程才会继续运行</p>
</li>
<li><p>主要方法</p>
<ol>
<li>构造函数CyclicBarrier（int parties，Runnable barrierAction）:用于在线程到达屏障时，优先执行barrierAction</li>
<li>默认构造函数CyclicBarrier（int parties）：屏障拦截的线程数<br>量，每个线程调用await方法告诉CyclicBarrier我已经到达了屏障，然后当前线程被阻塞</li>
<li>await:调用该方法的线程会被阻塞</li>
</ol>
</li>
<li><p>注意：线程池中使用CyclicBarrier ，要考虑线程被阻塞问题，</p>
</li>
<li><p>示例代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * @Description: 多任务结算银行流水</span><br><span class="line"> * @Auther: ThomasWu</span><br><span class="line"> * @Date: 2019/6/22 22:32</span><br><span class="line"> * @Email:1414924381@qq.com</span><br><span class="line"> */</span><br><span class="line">public class CyclicBarrierTest &#123;</span><br><span class="line">    static class BankWaterTask implements Runnable &#123;</span><br><span class="line">        private ConcurrentHashMap&lt;String, Integer&gt; sheetBankWaterCount = new ConcurrentHashMap&lt;String, Integer&gt;();</span><br><span class="line">        private CyclicBarrier c = new CyclicBarrier(4, this);</span><br><span class="line">        private Executor executor = Executors.newFixedThreadPool(4);</span><br><span class="line"></span><br><span class="line">        private void count() &#123;</span><br><span class="line">            System.out.println(&quot;count 开始&quot; + System.currentTimeMillis());</span><br><span class="line">            for (int i = 0; i &lt; 4; i++) &#123;</span><br><span class="line">                executor.execute(() -&gt; &#123;</span><br><span class="line">                    sheetBankWaterCount.put(Thread.currentThread().getName(), 1);</span><br><span class="line">                    try &#123;</span><br><span class="line">                        c.await();</span><br><span class="line">                    &#125; catch (InterruptedException | BrokenBarrierException e) &#123;</span><br><span class="line">                        e.printStackTrace();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(&quot;count 结束&quot; + System.currentTimeMillis());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        @Override</span><br><span class="line">        public void run() &#123;</span><br><span class="line">            System.out.println(&quot;run&quot; + System.currentTimeMillis());</span><br><span class="line">            int result = 0;</span><br><span class="line">            for (Map.Entry&lt;String, Integer&gt; sheet : sheetBankWaterCount.entrySet()) &#123;</span><br><span class="line">                result += sheet.getValue();</span><br><span class="line">            &#125;</span><br><span class="line">            sheetBankWaterCount.put(&quot;result&quot;, result);</span><br><span class="line">            System.out.println(result);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">   public static void main(String[] args) &#123;</span><br><span class="line">        BankWaterTask task = new BankWaterTask();</span><br><span class="line">        task.count();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="CyclicBarrier和CountDownLatch的区别"><a href="#CyclicBarrier和CountDownLatch的区别" class="headerlink" title="CyclicBarrier和CountDownLatch的区别"></a>CyclicBarrier和CountDownLatch的区别</h3><ul>
<li><p>CountDownLatch：调用countDown的线程（A线程），调用了await的线程（B线程），A线程先执行，B线程阻塞，满足条件（计数器为0时）B线程开始执行，没有被阻塞；</p>
</li>
<li><p>CyclicBarrier：构造函数传入barrierAction线程（A线程） ，调用了await的线程（B线程），A线程先阻塞，B线程也阻塞，满足条件（B线程都调用都调用await后），A线程开始执行完后，B线程开始执行</p>
</li>
<li><p>CyclicBarrier：barrierAction只能指定一个线程</p>
</li>
<li><p>CountDownLatch的计数器只能使用一次，而CyclicBarrier的计数器可以使用reset()方法重置</p>
</li>
<li><p>CyclicBarrier还提供其他有用的方法</p>
<ol>
<li>getNumberWaiting方法可以获得Cyclic-Barrier阻塞的线程数量</li>
<li>isBroken()方法用来了解阻塞的线程是否被中断</li>
</ol>
<h2 id="Semaphore"><a href="#Semaphore" class="headerlink" title="Semaphore"></a>Semaphore</h2></li>
<li><p>Semaphore的作用：用来控制同时访问特定资源的线程数量，它通过协调各个线程，以<br>保证合理的使用公共资源</p>
</li>
<li><p>使用场景： Semaphore可以用于做流量控制，特别是公用资源有限的应用场景，比如数据库连接。假<br>如有一个需求，要读取几万个文件的数据，因为都是IO密集型任务，我们可以启动几十个线程<br>并发地读取，但是如果读到内存后，还需要存储到数据库中，而数据库的连接数只有10个，这<br>时我们必须控制只有10个线程同时获取数据库连接保存数据，否则会报错无法获取数据库连<br>接。这个时候，就可以使用Semaphore来做流量控制</p>
</li>
<li><p>主要方法：</p>
<ol>
<li>Semaphore（int permits）：接受一个整型的数字，表示可用的许可证数量</li>
<li>void acquire()：获取一个许可证，如果当前信号量大于0，则当前信号量的计数减1，该方法直接返回；如果当前信号量等于0，则当前线程会被放入AQS阻塞队列</li>
<li>void release():将当前信号量加1</li>
</ol>
</li>
</ul>
<p>示例代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @Description: TODO</span><br><span class="line"> * @Auther: ThomasWu</span><br><span class="line"> * @Date: 2019/6/23 15:19</span><br><span class="line"> * @Email:1414924381@qq.com</span><br><span class="line"> */</span><br><span class="line">public class SemaphoreTest &#123;</span><br><span class="line">    private static final int THREAD_COUNT = 10;</span><br><span class="line">    private static ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);</span><br><span class="line">    private static Semaphore s = new Semaphore(5);</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        for (int i = 0; i &lt; THREAD_COUNT; i++) &#123;</span><br><span class="line">            threadPool.execute(() -&gt; &#123;</span><br><span class="line">                try &#123;</span><br><span class="line">                    s.acquire();</span><br><span class="line">                    System.out.println(Thread.currentThread().getName()+&quot;获得了信号量&quot;+&quot;---当前信号量:&quot;+s.availablePermits()+&quot;---等待信号量的线程数:&quot;+s.getQueueLength()+&quot;---&quot;+System.currentTimeMillis());</span><br><span class="line">                    Thread.sleep(2000);</span><br><span class="line">                &#125; catch (InterruptedException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;finally &#123;</span><br><span class="line">                    s.release();</span><br><span class="line">                    System.out.println(Thread.currentThread().getName()+&quot;释放了信号量------&quot;+&quot;---当前信号量:&quot;+s.availablePermits()+&quot;---等待信号量的线程数:&quot;+s.getQueueLength()+System.currentTimeMillis());</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;</span><br><span class="line">        threadPool.shutdown();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">console result</span><br><span class="line">pool-1-thread-2获得了信号量---当前信号量:4---等待信号量的线程数:0---1561275554694</span><br><span class="line">pool-1-thread-1获得了信号量---当前信号量:3---等待信号量的线程数:0---1561275554694</span><br><span class="line">pool-1-thread-4获得了信号量---当前信号量:2---等待信号量的线程数:0---1561275554694</span><br><span class="line">pool-1-thread-3获得了信号量---当前信号量:1---等待信号量的线程数:0---1561275554694</span><br><span class="line">pool-1-thread-5获得了信号量---当前信号量:0---等待信号量的线程数:0---1561275554694</span><br><span class="line">pool-1-thread-6获得了信号量---当前信号量:0---等待信号量的线程数:4---1561275556695</span><br><span class="line">pool-1-thread-7获得了信号量---当前信号量:0---等待信号量的线程数:3---1561275556695</span><br><span class="line">pool-1-thread-4释放了信号量---------当前信号量:0---等待信号量的线程数:31561275556695</span><br><span class="line">pool-1-thread-1释放了信号量---------当前信号量:0---等待信号量的线程数:31561275556695</span><br><span class="line">pool-1-thread-8获得了信号量---当前信号量:0---等待信号量的线程数:2---1561275556695</span><br><span class="line">pool-1-thread-2释放了信号量---------当前信号量:0---等待信号量的线程数:21561275556695</span><br><span class="line">pool-1-thread-9获得了信号量---当前信号量:1---等待信号量的线程数:0---1561275556696</span><br><span class="line">pool-1-thread-3释放了信号量---------当前信号量:0---等待信号量的线程数:01561275556696</span><br><span class="line">pool-1-thread-5释放了信号量---------当前信号量:1---等待信号量的线程数:01561275556696</span><br><span class="line">pool-1-thread-10获得了信号量---当前信号量:0---等待信号量的线程数:0---1561275556696</span><br><span class="line">pool-1-thread-6释放了信号量---------当前信号量:1---等待信号量的线程数:01561275558695</span><br><span class="line">pool-1-thread-8释放了信号量---------当前信号量:3---等待信号量的线程数:01561275558695</span><br><span class="line">pool-1-thread-7释放了信号量---------当前信号量:2---等待信号量的线程数:01561275558695</span><br><span class="line">pool-1-thread-10释放了信号量---------当前信号量:5---等待信号量的线程数:01561275558696</span><br><span class="line">pool-1-thread-9释放了信号量---------当前信号量:5---等待信号量的线程数:01561275558696</span><br></pre></td></tr></table></figure>

<h2 id="Exchanger"><a href="#Exchanger" class="headerlink" title="Exchanger"></a>Exchanger</h2><ul>
<li>Exchanger作用：用于进行线程间的数据交<br>换。它提供一个同步点，在这个同步点，两个线程可以交换彼此的数据。这两个线程通过<br>exchange方法交换数据，如果第一个线程先执行exchange()方法，它会一直等待第二个线程也<br>执行exchange方法，当两个线程都到达同步点时，这两个线程就可以交换数据，将本线程生产<br>出来的数据传递给对方。<ul>
<li>注意：如果两个线程有一个没有执行exchange()方法，则会一直等待，如果担心有特殊情况发<br>生，避免一直等待，可以使用exchange（V x，longtimeout，TimeUnit unit）设置最大等待时长</li>
</ul>
</li>
</ul>
<p> 示例代码：<br> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * @Description: TODO</span><br><span class="line"> * @Auther: ThomasWu</span><br><span class="line"> * @Date: 2019/6/23 15:53</span><br><span class="line"> * @Email:1414924381@qq.com</span><br><span class="line"> */</span><br><span class="line">public class ExchangerTest &#123;</span><br><span class="line"></span><br><span class="line">    private static final Exchanger&lt;String&gt; exchanger = new Exchanger&lt;String&gt;();</span><br><span class="line">    private static ExecutorService threadPool = Executors.newFixedThreadPool(2);</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        threadPool.execute(new Runnable() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void run() &#123;</span><br><span class="line">                try &#123;</span><br><span class="line">                    // A录入银行流水数据</span><br><span class="line">                    String str = &quot;银行流水A&quot;;</span><br><span class="line">                    System.out.println(Thread.currentThread().getName() + str);</span><br><span class="line">                    str = exchanger.exchange(str);</span><br><span class="line">                    System.out.println(Thread.currentThread().getName() + str);</span><br><span class="line">                &#125; catch (InterruptedException e) &#123;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        threadPool.execute(new Runnable() &#123;</span><br><span class="line">            @Override</span><br><span class="line">            public void run() &#123;</span><br><span class="line">                try &#123;</span><br><span class="line">                    String str = &quot;银行流水B&quot;;</span><br><span class="line">                    System.out.println(Thread.currentThread().getName() + str);</span><br><span class="line">                    str = exchanger.exchange(str);</span><br><span class="line">                    System.out.println(Thread.currentThread().getName() + str);</span><br><span class="line"></span><br><span class="line">                &#125; catch (InterruptedException e) &#123;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        threadPool.shutdown();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <div class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="Thomas Wu">
            
              <p class="site-author-name" itemprop="name">Thomas Wu</p>
              <div class="site-description motion-element" itemprop="description">人帅也要多读书，多学习，多运动</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">24</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">9</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">6</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/peach-boy" title="GitHub &rarr; https://github.com/peach-boy" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="/1414924381@qq.com" title="E-Mail &rarr; 1414924381@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/xrq730/" title="https://www.cnblogs.com/xrq730/" rel="noopener" target="_blank">五月的仓颉</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.w3cschool.cn/architectroad/" title="https://www.w3cschool.cn/architectroad/" rel="noopener" target="_blank">沈剑架构师之路</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://crossoverjie.top/" title="https://crossoverjie.top/" rel="noopener" target="_blank">crossoverjie</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.jianshu.com/u/2766e4cfc391" title="https://www.jianshu.com/u/2766e4cfc391" rel="noopener" target="_blank">高广超</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Thomas Wu</span>

  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.2"></script>

  <script src="/js/motion.js?v=7.1.2"></script>



  
  


  <script src="/js/affix.js?v=7.1.2"></script>

  <script src="/js/schemes/pisces.js?v=7.1.2"></script>




  

  


  <script src="/js/next-boot.js?v=7.1.2"></script>


  

  

  

  



  




  

  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
